package com.fone.player.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.fone.player.R;
import com.fone.player.activity.personal.UserInfoActivity;
import com.fone.player.client.Callback;
import com.fone.player.client.Error;
import com.fone.player.client.Request;
import com.fone.player.client.Rst;
import com.fone.player.sns.UserInfoManager;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.util.LoginUtil;
import com.fone.player.util.ScreenUtil;

public class ChangeUserNameDialog implements OnClickListener {
	
	private static final String TAG = "ChangeUserNameDialog";
	
	private EditText change_name_et;
	private UserInfoActivity mActivity;
	private CustomDialog dialog;
	private TextView title;
	
	private String tempUserName;
	
	public ChangeUserNameDialog(Context mContext, UserInfoActivity activity) {
		mActivity = activity; 
		
		View changeNameView = LayoutInflater.from(mContext).inflate(
				R.layout.personal_change_name_dialog, null);
		dialog = new CustomDialog((Context) activity,
				changeNameView);
		dialog.setCanceledOnTouchOutside(false);

		dialog.show();

		changeNameView.setMinimumWidth((int) (ScreenUtil
				.getScreenWidthPix(mContext) * 0.8));// 设置dialog的宽度

		change_name_et = (EditText) changeNameView
				.findViewById(R.id.change_name_et);

		title = (TextView) changeNameView.findViewById(R.id.change_name_title);
		title.setOnClickListener(this);
		title.setText("修改用户名");
		
		change_name_et.setHint(R.string.regist_username_text);
		
		Button changeSureBtn = (Button) changeNameView
				.findViewById(R.id.change_name_sure_id);
		changeSureBtn.setOnClickListener(this);

		Button changeCancelBtn = (Button) changeNameView
				.findViewById(R.id.change_name_cancel_id);
		changeCancelBtn.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.change_name_sure_id:
			if(FoneUtil.isNetOkWithToast(mActivity.getApplicationContext())){
				try {
					String username = change_name_et.getText().toString();
					if(!LoginUtil.checkUserName(username)){
						Toast.makeText(mActivity.getApplicationContext(), "用户名不符合规则", Toast.LENGTH_SHORT).show();
						return;
					}
					tempUserName = username;
					String newvalue = FoneUtil.Base64Encode(username);
					mActivity.mHandler.sendEmptyMessage(UserInfoActivity.SHOW_WAITTING);
					Request.getInstance().updateuser(2, "", newvalue, new changeUserNameCallback());
				} catch (Exception e) {
					e.printStackTrace();
					L.e(TAG, "onClick", e.getMessage());
				}
				dialog.cancel();
			}
			break;
		case R.id.change_name_cancel_id:
			dialog.cancel();
			break;

		default:
			break;
		}
	}

	
	private class changeUserNameCallback implements Callback<Rst>{

		@Override
		public void onSuccess(Rst t) {
			L.v(TAG, "changeUserNameCallback onSuccess", t.toString());
			if(t.result!=0){
				if(t.error!=null){
					L.v(TAG, "onSuccess","errorcode : "+ t.error.errorcode);
					try{
						switch (Integer.valueOf(t.error.errorcode)) {
						case 117:
							FoneUtil.showToast(mActivity.getApplicationContext(), "用户名输入有误");
							break;
						default:
							FoneUtil.showToast(mActivity.getApplicationContext(), "修改失败");
							break;
						}
					}catch(Exception e){
						FoneUtil.showToast(mActivity.getApplicationContext(), "修改失败");
					}
					mActivity.mHandler.sendEmptyMessage(UserInfoActivity.CANCEL_WAITTING);
					tempUserName = null;
					return;
				}
			}
			new Thread(){
				public void run() {
					UserInfoManager.saveUserid(tempUserName);
					UserInfoManager.saveIsModified("1");
					mActivity.mHandler.obtainMessage(UserInfoActivity.CHANGE_USERNAME_SUCCESS, new String(tempUserName)).sendToTarget();
					tempUserName = null;
				};
			}.start();
		}

		@Override
		public void onFailure(Error error) {
			mActivity.mHandler.sendEmptyMessage(UserInfoActivity.CANCEL_WAITTING);
			L.v(TAG,
					"changeUserNameCallback onFailure",
					"isNetworkError : " + error.isNetworkError() + " status : "
							+ error.getStatus() + " reason : "
							+ error.getReason());
			mActivity.mHandler.sendEmptyMessage(UserInfoActivity.CANCEL_WAITTING);
			tempUserName = null;
			
			if(error.isNetworkError()){
				FoneUtil.showToast(mActivity.getApplicationContext(), "网络超时,请稍后重试");
				return ;
			}
			FoneUtil.showToast(mActivity.getApplicationContext(), "修改失败");
			tempUserName = null;
		}
		
	}
	
}
